package com.suxin.logger.formatter;

import com.suxin.logger.LogRecord;

import java.text.MessageFormat;

/**
 * @author Tang
 * @classname Formatter
 * @description [  ]
 * @date 2022/4/26 22:11
 */
public abstract class Formatter {

    /**
     * format
     * @param lr
     * @return
     */
    public abstract String format(LogRecord lr);

    /**
     * formatMessage
     * @param record
     * @return
     */
    public String formatMessage(LogRecord record) {
        String recordMsg = record.getMsg();
        Object[] parameters = record.getParameters();
        if (parameters == null || parameters.length == 0) {
            return recordMsg;
        }
        // 格式化
        if (recordMsg.indexOf("{0") >= 0 || recordMsg.indexOf("{1") >= 0
          || recordMsg.indexOf("{2") >= 0) {
            return MessageFormat.format(recordMsg, parameters);
        }
        return recordMsg;
    }

}
